import Vue from 'vue';
import Router from 'vue-router';

/**
 * 修复Vue-Router升级导致的Uncaught (in promise)问题
 */
const originalPush = Router.prototype.push;
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(Router);


/* Router Modules */
const modulesFiles = require.context("./modules", true, /\.js$/);
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  const value = modulesFiles(modulePath);
  if (value.default != undefined) {
    modules.push(value.default);
  }
  return modules;
}, []);

console.log('modules', modules)



/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    //首页组件
    path: '/',
    redirect: '/Home',
    component: Layout,
    meta: { title: '首页' },
    name: "Home",
    children: [
      {
        path: '/Home',
        name: 'Home',
        component: resolve => require(['@/views/Home/index.vue'], resolve),
        meta: { title: '首页' }
      }
    ]
  },
  ...modules

  // {
  //   // 写文章
  //   path: '/write',
  //   component: resolve => require(['../components/page/Write.vue'], resolve),
  //   meta: { title: '写文章' }
  // },


]


export default new Router({
  routes: constantRoutes
})